/*
 * @Description: 展开收缩动画组件
 * @Version: V1.0.0
 * @Author: 黄鑫
 * @Date: 2025-05-30
 * @LastEditors: 黄鑫
 * @LastEditTime: 2025-09-29
 * @FilePath: ExpandAnimationView.dart
 * @Remarks: 
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hx_base/resource/Assets.dart';

class ExpandAnimationView extends StatefulWidget {
  ///头部view
  Widget titleView;

  ///点击动画view
  Widget? iconView;

  ///内容view
  Widget contentView;

  ///**
  /// 控制尺寸缩放时的对齐方式：
  ///-1.0：从顶部/左侧开始
  ///0.0：中间
  ///1.0：从底部/右侧开始 */
  double? axisAlignment;

  ///头部装饰
  EdgeInsets? titleMargin;

  EdgeInsets? titlePadding;

  BoxDecoration? titleDecoration;

  // 展开 默认false
  bool? isExpand;

  ///装饰
  EdgeInsets? margin;

  EdgeInsets? padding;

  BoxDecoration? decoration;

  ExpandAnimationView({
    super.key,
    required this.titleView,
    required this.contentView,
    this.iconView,
    this.axisAlignment,
    this.margin,
    this.isExpand,
    this.padding,
    this.decoration,
    this.titleMargin,
    this.titlePadding,
    this.titleDecoration,
  });

  @override
  _ExpandAnimationViewState createState() => _ExpandAnimationViewState();
}

class _ExpandAnimationViewState extends State<ExpandAnimationView>
    with SingleTickerProviderStateMixin {
  ///动画控制器
  late AnimationController _controller;

  ///动画效果
  late Animation<double> _sizeAnimation;

  /// 是否显示 默认不显示
  var isExpand = false.obs;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 300),
      vsync: this,
    );

    _sizeAnimation = CurvedAnimation(
      parent: _controller,
      curve: Curves.easeInOut,
    );
    isExpand.value = widget.isExpand ?? false;
    if (isExpand.value) {
      _controller.forward();
    } else {
      _controller.reverse();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: widget.margin ?? EdgeInsets.fromLTRB(10, 10, 10, 10),
      padding: widget.padding ?? EdgeInsets.zero,
      decoration: widget.decoration ??
          BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10.r),
          ),
      clipBehavior: Clip.hardEdge,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: widget.titleMargin,
            padding: widget.titlePadding,
            decoration: widget.titleDecoration,
            child: Row(
              children: [
                Expanded(child: widget.titleView),
                widget.iconView != null
                    ? GestureDetector(
                        onTap: () {
                          isExpand.value = !isExpand.value;
                          if (isExpand.value) {
                            _controller.forward();
                          } else {
                            _controller.reverse();
                          }
                        },
                        child: widget.iconView,
                      )
                    : IconButton(
                        onPressed: () {
                          isExpand.value = !isExpand.value;
                          if (isExpand.value) {
                            _controller.forward();
                          } else {
                            _controller.reverse();
                          }
                        },
                        style: ButtonStyle(
                          padding: WidgetStatePropertyAll(EdgeInsets.all(10)),
                          minimumSize: WidgetStatePropertyAll(Size(0, 0)),
                          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                        ),
                        icon: Obx(() => Image.asset(
                              isExpand.value
                                  ? Assets.imagesHxArrowUpIcon
                                  : Assets.imagesHxArrowDownIcon,
                              package: "hx_base",
                              width: 15.w,
                              height: 15.w,
                            )),
                      )
              ],
            ),
          ),
          contentView(),
        ],
      ),
    );
  }

  contentView() {
    return ClipRect(
      child: SizeTransition(
        sizeFactor: _sizeAnimation,
        axisAlignment: widget.axisAlignment ?? -1.0, // 从上往下展开/收起
        child: widget.contentView,
      ),
    );
  }
}
